﻿#region Copyright (c) 2012-11, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_ParallelismLevelAttributes
	{
		[AttributeUsage(AttributeTargets.Method)]
		public class MyParallelismLevelAttribute : ParallelismLevelAttribute
		{
			public MyParallelismLevelAttribute()
				: base(-123)
			{
			}

			public MyParallelismLevelAttribute(Int32 parallelismLevel)
				: base(parallelismLevel)
			{
			}
		}

		private abstract class MyBaseClass
		{
			[ModerateParallelism]
			public abstract void TestMethod6();

			public abstract void TestMethod7();
		}

		private class MyClass : MyBaseClass
		{
			public void TestMethod1()
			{
			}

			[NoParallelism]
			public void TestMethod2()
			{
			}

			[MinimumParallelism]
			public void TestMethod3()
			{
			}

			[ModerateParallelism]
			public void TestMethod4()
			{
			}

			[MaximumParallelism]
			public void TestMethod5()
			{
			}

			public override void TestMethod6()
			{
			}

			[NoParallelism]
			public override void TestMethod7()
			{
			}

			[OrderedProcessing]
			public void TestMethod8()
			{
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_GetParallelismLevel_NoAttribute()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod1");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123), Is.EqualTo(123));
		}

		[Test]
		public void Test_GetParallelismLevel_NoParallelism()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod2");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123),
				Is.EqualTo(ParallelismLevel.NoParallelism));
		}

		[Test]
		public void Test_GetParallelismLevel_MinimumParallelism()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod3");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123),
				Is.EqualTo(ParallelismLevel.MinimumParallelism));
		}

		[Test]
		public void Test_GetParallelismLevel_ModerateParallelism()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod4");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123),
				Is.EqualTo(ParallelismLevel.ModerateParallelism));
		}

		[Test]
		public void Test_GetParallelismLevel_MaximumParallelism()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod5");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123),
				Is.EqualTo(ParallelismLevel.MaximumParallelism));
		}

		[Test]
		public void Test_GetParallelismLevel_AbstractMethod_AttributeOnBaseType()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod6");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123),
				Is.EqualTo(ParallelismLevel.ModerateParallelism));
		}

		[Test]
		public void Test_GetParallelismLevel_AbstractMethod_AttributeOnOverridingMethod()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod7");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123),
				Is.EqualTo(ParallelismLevel.NoParallelism));
		}

		[Test]
		public void Test_GetParallelismLevel_OrderedProcessing()
		{
			var target = new MyClass();
			var targetType = target.GetType();
			var method = targetType.GetMethod("TestMethod8");

			Assert.That(method, Is.Not.Null);
			Assert.That(ParallelismLevelAttribute.GetParallelismLevel(method, 123),
				Is.EqualTo(ParallelismLevel.NoParallelism));
		}

		[Test]
		public void Test_GetParallelismLevel_NullMethod()
		{
			Assert.Throws<ArgumentNullException>(() =>
				{
					ParallelismLevelAttribute.GetParallelismLevel(null, 123);
				});
		}

		[Test]
		public void Test_Construction()
		{
			Assert.That(new MyParallelismLevelAttribute(-1).ParallelismLevel, Is.EqualTo(ParallelismLevel.MaximumParallelism));
			Assert.That(new MyParallelismLevelAttribute(0).ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));
			Assert.That(new MyParallelismLevelAttribute(1).ParallelismLevel, Is.EqualTo(1));
			Assert.That(new MyParallelismLevelAttribute(2).ParallelismLevel, Is.EqualTo(2));
			Assert.That(new MyParallelismLevelAttribute(3).ParallelismLevel, Is.EqualTo(3));
		}

	}
}

#endif
